﻿using HZY.Framework.DependencyInjection;
using Microsoft.Extensions.Logging;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LogLevel = Microsoft.Extensions.Logging.LogLevel;

namespace Apple.SprayingProcessSystem
{
    public partial class PageTotalEquipmentControl : UIPage, ISingletonSelfDependency
    {
        private readonly ILogger<PageTotalEquipmentControl> _logger;

        public PageTotalEquipmentControl(ILogger<PageTotalEquipmentControl> _logger)
        {
            InitializeComponent();
            this._logger = _logger;
        }

        private void btn_Control_Common_Click(object sender, EventArgs e)
        {
            if (!Globals.SiemensClient.Connected)
            {
                UIMessageTip.ShowError("请先连接西门子PLC!");
                return;
            }

            UISymbolButton btn = sender as UISymbolButton;

            if (Globals.PlcWrite(btn.TagString, true))
            {
                UIMessageTip.ShowOk($"{btn.TagString}写入成功!");
                ShowLog($"写入{btn.TagString}成功");
            }
            else
            {
                UIMessageTip.ShowError($"{btn.TagString}写入失败!");
                ShowLog($"写入{btn.TagString}失败");
            }
        }

        private void btn_DryRun_ClickEvent(object sender, EventArgs e)
        {
            if (!Globals.SiemensClient.Connected)
            {
                UIMessageTip.ShowError("请先连接西门子PLC!");
                return;
            }

            if (Globals.PlcWrite(this.btn_DryRun.VariableName, !this.btn_DryRun.CounterButtonState))
            {
                this.btn_DryRun.CounterButtonState = !this.btn_DryRun.CounterButtonState;
                UIMessageTip.ShowOk("写入成功");
                ShowLog($"写入{btn_DryRun.VariableName}成功");
            }
            else
            {
                UIMessageTip.ShowError("写入失败");
                ShowLog($"写入{btn_DryRun.VariableName}失败");
            }
        }

        private void device_Common_ClickEvent(object sender, EventArgs e)
        {
            UserDeviceUnitControl userDeviceUnit = sender as UserDeviceUnitControl;

            if (!Globals.SiemensClient.Connected)
            {
                userDeviceUnit.State = false; // 没有连接,设置状态为关闭
                UIMessageTip.ShowError("请先连接西门子PLC!");
                return;
            }

            bool state = userDeviceUnit.State;

            string variableName = state ? userDeviceUnit.CloseVariableName : userDeviceUnit.OpenVariableName;

            if (Globals.PlcWrite(variableName, state))
            {
                UIMessageTip.ShowOk("写入成功");
                ShowLog($"写入{variableName}成功");
            }
            else
            {
                UIMessageTip.ShowError("写入失败");
                ShowLog($"写入{variableName}失败");
            }
        }

        private void ShowLog(string log, LogLevel logLevel = LogLevel.Information)
        {
            this.txt_Log.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "=>" + log + Environment.NewLine);

            switch (logLevel)
            {
                case LogLevel.Information:
                    _logger.LogInformation(log);
                    break;
                case LogLevel.Warning:
                    _logger.LogWarning(log);
                    break;
                case LogLevel.Error:
                    _logger.LogError(log);
                    break;
            }

        }

    }
}
